package com.mukefile.dao.impl;

import com.mukefile.dao.MukeFileDao;
import com.mukefile.pojo.MukeFile;
import com.mukefile.pojo.User;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Author: kenewstar
 * @Description:
 * @Date:Created in 2020/5/6
 */
@Repository("mukeFileDao")
public class MukeFileDaoImpl implements MukeFileDao {
    @Autowired
    private HibernateTemplate hibernateTemplate;
    @Override
    public int fileUpload(File file, MukeFile mukeFile) {
        //修改数据库信息
        //添加一条信息
        try {
            this.hibernateTemplate.save(mukeFile);
            this.hibernateTemplate.update(mukeFile.getUser());
        }catch (Exception e){
            //出异常返回0
            return 0;
        }
        return 1;
    }

    @Override
    public List<MukeFile> selectAllFile(User user) {
        User user1 = this.hibernateTemplate.get(User.class, user.getId());
        List<MukeFile> list = new ArrayList<>();
        for (MukeFile mukeFile : user1.getMukeFiles()){
            list.add(mukeFile);
        }
        return list;
    }

    @Override
    public int deleteFile(MukeFile mukeFile) {
        try{
            this.hibernateTemplate.delete(mukeFile);
            this.hibernateTemplate.update(mukeFile.getUser());
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
}
